package com.hnjd.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hnjd.dao.IndexDao;
import com.hnjd.entity.Carts;
import com.hnjd.entity.Goods;
import com.hnjd.entity.Items;
import com.hnjd.entity.Orders;
import com.hnjd.entity.Tops;
import com.hnjd.entity.Users;
import com.hnjd.service.IindexService;
import com.hnjd.util.PageUtil;

@Service
public class IndexService implements IindexService{

	@Autowired
	private IndexDao dao;

	@Override
	public List<Goods> indexList() {
		// TODO Auto-generated method stub
		return dao.indexList();
	}

	@Override
	public Users login(String username, String password) {
		// TODO Auto-generated method stub
		return dao.login(username, password);
	}

	@Override
	public Integer register(Users users) {
		// TODO Auto-generated method stub
		return dao.register(users);
	}
	
	@Override
	public Integer addressUpdate(Orders orders) {
		// TODO Auto-generated method stub
		return dao.addressUpdate(orders);
	}
	
	@Override
	public int[] ordersById(String name) {
		// TODO Auto-generated method stub
		return dao.ordersById(name);
	}
	
	@Override
	public Integer passwordUpdate(String passwordNew, Integer id) {
		// TODO Auto-generated method stub
		return dao.passwordUpdate(passwordNew, id);
	}
	
	@Override
	public List<Orders> orderList() {
		// TODO Auto-generated method stub
		return dao.orderList();
	}
	
	@Override
	public Goods itemsByGoodsId(Integer id) {
		// TODO Auto-generated method stub
		return dao.itemsByGoodsId(id);
	}
	
	@Override
	public List<Items> itemsByOrderId(Integer order_id) {
		// TODO Auto-generated method stub
		return dao.itemsByOrderId(order_id);
	}

	@Override
	public List<Carts> cartsByUserId(Integer user_id,PageUtil page) {
		// TODO Auto-generated method stub
		return dao.cartsByUserId(user_id,page);
	}
	
	@Override
	public Goods goodsById(Integer id) {
		// TODO Auto-generated method stub
		return dao.goodsById(id);
	}
	
	@Override
	public Integer[] orderByUserId(Integer user_id) {
		// TODO Auto-generated method stub
		return dao.orderByUserId(user_id);
	}
	@Override
	public Integer cartsNum(Integer user_id) {
		// TODO Auto-generated method stub
		return dao.cartsNum(user_id);
	}
	
	@Override
	public Integer cartUpdate(Integer id, Integer amount) {
		// TODO Auto-generated method stub
		return dao.cartUpdate(id, amount);
	}
	
	@Override
	public Integer cartDelete(Integer id) {
		// TODO Auto-generated method stub
		return dao.cartDelete(id);
	}
	
	@Override
	public List<Carts> cartsByUserList(Integer user_id) {
		// TODO Auto-generated method stub
		return dao.cartsByUserList(user_id);
	}
	//添加购物车
    public Integer addCart(Integer goodid,Integer userid) {
    	
    	return dao.addCart(goodid, userid);
    }
    
    
    //判断购物车中是否含有这个商品
    public Integer cartExict(Integer goodid,Integer userid ) {
    	
    	return  dao.cartExict(goodid, userid);
    }
    
    //存在相同商品amount加一
    public Integer addAmount(Integer goodid,Integer userid ) {
    	
    	return dao.addAmount(goodid, userid);
    }
    
    @Override
	public List<Tops> findAllTops() {
		// TODO Auto-generated method stub
		return dao.findAllTops();
	}
	@Override
	public void topSave(Tops top) {
		Integer goodId = null;
		Integer id=null;
		// TODO Auto-generated method
		List<Goods> list = dao.findGoodsById();
		List<Tops> list2 = dao.findAllTops();
		for (Tops tops : list2) {
			goodId = tops.getGoodId();
		}
		for (Goods good : list) {
			 id = good.getId();
		}
		if(goodId!=id) {
			dao.topSave(top);
		}
		
		
	}
	@Override
	public void topDelete(Integer goodId) {
		// TODO Auto-generated method stub
		Integer goodId1 = null;
		Integer tid=null;
		// TODO Auto-generated method
		List<Goods> list = dao.findGoodsById();
		List<Tops> list2 = dao.findAllTops();
		for (Tops tops : list2) {
			goodId1 = tops.getGoodId();
		}
		for (Goods good : list) {
			 tid = good.getId();
		}
		if(goodId1==tid) {
			dao.topDelete(goodId);
		}
	
	}
	@Override
	public List<Goods> findGoodsById() {
		// TODO Auto-generated method stub
		return  dao.findGoodsById();
	}
	@Override
	public List<Goods> findGoods(PageUtil page) {
		// TODO Auto-generated method stub
		return dao.findGoods(page);
	}
	
	
	     //分页查询 今日推荐
		public Object findGoodsTop(int curPage,int pageSize){
			Page<Goods> page = PageHelper.startPage(curPage,pageSize);
			List<Goods> goods = dao.findGoodsById();
			PageInfo<Goods> pageInfo = new PageInfo<Goods>(goods);
			return pageInfo;
		}
	
	   //查询所有热销商品
		public  Object findSalesGoods(int curPage,int pageSize){
			  Page<Goods> page = PageHelper.startPage(curPage, pageSize);
			  
			  List<Goods> goods = dao.findSalesGoods();
			  
			  PageInfo<Goods> pageInfo=new PageInfo<Goods>(goods);
			  
			  return pageInfo;
			 
		}
		
		@Transactional  //事物：保证所有的sql语句都执行成功，如果有一条失败就全部回撤
		public boolean generetorOrder(Users user) {
			
			//查询购物车的信息
			List<Carts> list=dao.findCartsByUserId(user.getId());
			
			//生成订单
			//计算两个数值  total：总价钱   amount:总数量
			int total=0;
			int amount=0;
			
			for (Carts carts : list) {
				amount+=carts.getAmount();
				total+=carts.getAmount()*carts.getGood().getPrice();
			}
			
			//调用dao层  1：生成一个order对象，传递order对象
			Orders order=new Orders();
			order.setTotal(total);
			order.setAmount(amount);
			order.setTotal(total);
			order.setName(user.getName());
			order.setAddress(user.getAddress());		
			order.setPhone(user.getPhone());
			order.setUserId(user.getId());
			dao.generetorOrder(order);
			//获取自动生成的主键
			int orderId = order.getId();
			
			//写入items表  需要新生成订单的orderid
			//购物车中有多少条商品，就需要添加多少条items信息
			for (Carts carts : list) {
				
				Items item = new Items();
				item.setPrice(carts.getGood().getPrice());
				item.setAmount(carts.getAmount());
				item.setOrder_id(orderId);
				item.setGood_id((carts.getGood_id()));
				
				//调用itemsDao的新增方法
				dao.add(item);			
			}		
			
			//清空购物车
			dao.DeleteByUserId(user.getId());		
			
			return true;
		}
}
